package com.bl.student.api.controller;

import com.bl.student.common.ApiResponseBody;
import com.bl.student.dao.model.CityEntity;
import com.bl.student.service.biz.main.MainService;
import com.bl.student.service.biz.message.MessageService;
import com.bl.student.service.vo.ListVo;
import com.bl.student.service.vo.main.*;
import com.bl.student.service.vo.user.UserInfoModifyVo;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by yujingyi on 2018/1/17.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/painters/apis"})
@Api(value = "注册登陆相关api", description = "注册登陆相关api")
public class MainController extends BaseController {

    @Autowired
    MainService mainService;

    @Autowired
    MessageService messageService;

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_captcha"})
    public ApiResponseBody getCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile,
            HttpServletRequest request) {

        messageService.sendCaptchaMessage(mobile);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "获取国际短信验证码", notes = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_internation_captcha"})
    public ApiResponseBody getInternetionCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile,
            HttpServletRequest request) {
        messageService.sendInternetionCaptchaMessage(mobile);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "注册接口", notes = "")
    @PostMapping(value = {"/register"})
    @ResponseBody
    public ApiResponseBody<RegisterResponseVo> register(
            @Valid @RequestBody RegisterVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        RegisterResponseVo registerResponseVo = mainService.register(body);

        return ApiResponseBody.createSuccessBody(registerResponseVo);
    }

    @ApiOperation(value = "登陆", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/login"})
    public ApiResponseBody<LoginResponseVo> login(
            @Valid @RequestBody LoginVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.appLogin(body);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }


    @ApiOperation(value = "登陆username", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/login_by_name"})
    public ApiResponseBody<LoginResponseVo> loginByName(
            @Valid @RequestBody LoginVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.appLoginByName(body);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "修改密码", notes = "")
    @PostMapping(value = {"/reset"})
    public ApiResponseBody reset(
            @Valid @RequestBody ResetVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {
        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        mainService.reset(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "用户信息修改接口", notes = "")
    @PostMapping(value = {"/modify"})
    @ResponseBody
    public ApiResponseBody<UserInfoModifyVo> modify(
            @Valid @RequestBody UserInfoModifyVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        mainService.modifyUser(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "登出", notes = "")
    @ApiImplicitParams({})
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request, HttpServletResponse response) {
        mainService.logout(getUserInfo(request));
        return ApiResponseBody.createSuccessBody(null);
    }

}
